package com.ruoyi.partybuilding.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.easy.query.api4j.select.Queryable;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.partybuilding.common.EQueryHelper;
import com.ruoyi.partybuilding.common.PageSort;
import com.ruoyi.partybuilding.domain.SysDeptRef;
import com.ruoyi.partybuilding.domain.SysUserRef;
import com.ruoyi.partybuilding.domain.vo.DeptOrgVo;
import com.ruoyi.partybuilding.domain.vo.UserMemberVo;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import com.ruoyi.partybuilding.mapper.MemberMapper;
import com.ruoyi.partybuilding.domain.Member;
import com.ruoyi.partybuilding.service.IMemberService;

import javax.annotation.Resource;

/**
 * 党员信息Service业务层处理
 * 
 * @author he-liangliang@outlook.com
 * @date 2024-11-27
 */
@Service
public class MemberServiceImpl implements IMemberService 
{
    @Autowired
    private MemberMapper memberMapper;
    @Resource
OrgServiceImpl orgService;
    @Resource
EQueryHelper queryHelper;
    public R<List<UserMemberVo>> queryVo(UserMemberVo vo) {
        var page = voSelect()
                .whereObject(vo)
                .toList();
        return R.ok(page);
    }
    public Page<UserMemberVo> queryVo(UserMemberVo whereObj, boolean includeSubsDept, PageSort pageSort) {
        var deptId = whereObj.getDeptId();
        List<Long> ids;
        if (includeSubsDept && deptId != null) {
            whereObj.setDeptId(null);
            var depts = orgService.queryAndSubs(deptId).getData();
            ids = depts.stream().map(DeptOrgVo::getDeptId).collect(Collectors.toList());
        } else
            ids = null;
        var page = voSelect().whereObject(whereObj)
                .where(whereObj.getId() == null, p -> p.isNotNull(UserMemberVo::getId))
                .where(whereObj.getDelFlag() == null, p1 -> p1.eq(UserMemberVo::getDelFlag, 0))
                .where(ids != null, p -> p.in(UserMemberVo::getDeptId, ids))
                .orderByObject(pageSort)
                .toPageResult(pageSort.getPageNum(), pageSort.getPageSize());
        return PageSort.toPageData(page);
    }
    public R<UserMemberVo> queryById(Long userId) {
        var re = voSelect()
                .where(p -> p.eq(UserMemberVo::getUserId, userId))
                .firstOrNull();
        return R.ok(re);
    }
    public List<UserMemberVo> queryByIds(List<Long> userIds) {
        return voSelect().where(p->p.in(UserMemberVo::getUserId,userIds)).toList();
    }





    private Queryable<UserMemberVo> voSelect() {
        return queryHelper.queryable(Member.class).rightJoin(SysUserRef.class, (p, p2) -> p.eq(p2, Member::getUserId, SysUserRef::getUserId))
                .leftJoin(SysDeptRef.class,(p,p2,p3)->p2.eq(p3,SysUserRef::getDeptId,SysDeptRef::getDeptId))
                .select(UserMemberVo.class, (p, p2,p3) -> {
                    p.columnAll();
                    p2.columnAll();
                    p.columnIgnore(Member::getUserId);
                    p2.columnIgnore(SysUserRef::getPassword);
                    p3.column(SysDeptRef::getDeptName);
                    p3.column(SysDeptRef::getAncestors);
                });
    }

    /**
     * 查询党员信息
     * 
     * @param id 党员信息主键
     * @return 党员信息
     */
    @Override
    public Member selectMemberById(Long id)
    {
        return memberMapper.selectMemberById(id);
    }

    /**
     * 查询党员信息列表
     * 
     * @param member 党员信息
     * @return 党员信息
     */
    @Override
    public List<Member> selectMemberList(Member member)
    {
        return memberMapper.selectMemberList(member);
    }

    /**
     * 新增党员信息
     * 
     * @param member 党员信息
     * @return 结果
     */
    @Override
    public int insertMember(Member member)
    {
        return memberMapper.insertMember(member);
    }

    /**
     * 修改党员信息
     * 
     * @param member 党员信息
     * @return 结果
     */
    @Override
    public int updateMember(Member member)
    {
        return memberMapper.updateMember(member);
    }

    /**
     * 批量删除党员信息
     * 
     * @param ids 需要删除的党员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberByIds(Long[] ids)
    {
        return memberMapper.deleteMemberByIds(ids);
    }

    /**
     * 删除党员信息信息
     * 
     * @param id 党员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberById(Long id)
    {
        return memberMapper.deleteMemberById(id);
    }
}
